//
//  HomeViewController.m
//  AoJi
//
//  Created by MinwenYi on 15-1-20.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "MainViewController.h"
#import "ConsultViewController.h"
#import "PersonalViewController.h"
#import "ContactViewController.h"
#import "SettingViewController.h"

#import "LoginViewController.h"
#import "NSUserDefaultsKeys.h"

#include "TestViewController.h"
@interface HomeViewController ()<UITableViewDataSource,UITableViewDelegate>
{

    NSArray *titleArray;
    NSArray *imagesArray;
    int redRowNum;

    
}
@end

@implementation HomeViewController

- (void)viewDidLoad {
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTableview:) name:kSideMenuOpen object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginsuccessNI:) name:@"loginsuccess" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registsuccessNI:) name:@"registsuccess" object:nil];
    
    self.navigationController.navigationBarHidden=YES;
    [super viewDidLoad];
    
    titleArray=[NSArray arrayWithObjects:@"首页", @"咨询",@"个人中心",@"联系澳际",@"设置",nil];
    
    
    imagesArray=[NSArray arrayWithObjects:@"shouye0",@"xiaoxi0",@"gerengzhongxing0",@"lianxi0",@"shezhi0", nil];
    
    self.homeTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];
    
    [self.view addSubview:_homeTable];
    _homeTable.dataSource=self;
    _homeTable.delegate=self;
    _homeTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    _homeTable.backgroundColor=[UIColor clearColor];
    [_homeTable registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIView *tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _homeTable.frame.size.width, 200)];
    
    //侧边栏logo
    UIImageView *imageview= [[UIImageView alloc]initWithFrame:CGRectMake((viewWidth(tableHeaderView)-379/2)/2, 80, 379/2 ,122/2)];
    UIImage *scalelogoImg=[self scaleToSize:[UIImage imageNamed:@"sideMenulog.png"] size:CGSizeMake(379/2,122/2)];
    
    imageview.image=scalelogoImg;
    
    
    [tableHeaderView addSubview:imageview];
    
    
    _homeTable.tableHeaderView=tableHeaderView;
    
    UIImageView *bgImageVeiw=[[UIImageView alloc]initWithFrame:_homeTable.frame];
    [bgImageVeiw setImage:[UIImage imageNamed:@"homebg.png"]];
    _homeTable.backgroundView=bgImageVeiw;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)reloadTableview:(NSNotification*)notifiction
{
 
    redRowNum   =[[notifiction object] intValue];
    
    [self.homeTable reloadData];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return titleArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    HomeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor=[UIColor clearColor];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.titleLable.text=titleArray[indexPath.row];
    
    NSString *imgName=imagesArray[indexPath.row];
    NSString *lastName;
    if (indexPath.row==redRowNum) {
        cell.titleLable.textColor=[UIColor redColor];
       lastName= [imgName stringByAppendingString:@"2"];
    }else{
    lastName= [imgName stringByAppendingString:@"1"];
    cell.titleLable.textColor=[UIColor whiteColor];
    }
    
    cell.headImgView.image=[UIImage imageNamed:lastName];
    return cell;

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        MainViewController *testVc=[[MainViewController alloc]init];
//        testVc.loadtype=loadFromSideMenu;
        [self.drawer replaceCenterViewControllerWithViewController:testVc];
    }else  if(indexPath.row==1)
    {
        ConsultViewController *consultVC = [[ConsultViewController alloc]init];
        [self.drawer replaceCenterViewControllerWithViewController:consultVC];

    }
    else if(indexPath.row==2)
    {
        //(![[[NSUserDefaults standardUserDefaults] objectForKey:@"login"]  isEqualToString:@"yes"] || ![[[NSUserDefaults standardUserDefaults] objectForKey:@"regist"]  isEqualToString:@"yes"])
        
        if (![kUserDefaults objectForKey:kAccessToken]) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"亲,您还没有登陆嘞!" delegate:self cancelButtonTitle:@"马上去登陆" otherButtonTitles:@"再看看吧", nil];
            [alertView show];
        }else{
            PersonalViewController *personalVC = [[PersonalViewController alloc]init];
            [self.drawer replaceCenterViewControllerWithViewController:personalVC];
        }

    }
    else if(indexPath.row==3)
    {
        ContactViewController *contactVC=[[ContactViewController alloc]init];
        [self.drawer replaceCenterViewControllerWithViewController:contactVC];
    }
    else if(indexPath.row==4)
    {
        SettingViewController *settingVC=[[SettingViewController alloc]init];
        [self.drawer replaceCenterViewControllerWithViewController:settingVC];
    }
    
}

//跳转登陆
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self presentViewController:[[LoginViewController alloc]init] animated:NO completion:^{
            
        }];
//        [self.drawer replaceCenterViewControllerWithViewController:[[LoginViewController alloc]init]];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 65;
}
-(void)viewWillAppear:(BOOL)animated
{

 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
-(void)loginsuccessNI:(NSNotification*)nitifiction
{
    [self.drawer replaceCenterViewControllerWithViewController:[nitifiction object]];
}
-(void)registsuccessNI:(NSNotification*)nitifiction
{
    [self.drawer replaceCenterViewControllerWithViewController:[nitifiction object]];
}
@end
